﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-08-18 18:36:28Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;
using System.Windows.Controls;

using Outcoder.InversionOfControl;

namespace Outcoder.Services.Implementation
{
	public class FrameNavigationService : INavigationService
	{
		Frame frameUseProperty;

		Frame Frame
		{
			get
			{
				if (frameUseProperty == null)
				{
					//frameUseProperty = (Frame)Window.GetWindow(Current.Content);

					if (frameUseProperty == null)
					{
						throw new InvalidOperationException("The application does not contain a root frame.");
					}
				}
				return frameUseProperty;
			}
			set
			{
				frameUseProperty = value;
			}
		}

		public FrameNavigationService(Frame frame)
		{
			frameUseProperty = ArgumentValidator.AssertNotNull(frame, "frame");
		}

		[InjectDependencies]
		public FrameNavigationService()
		{
			/* Intentionally left blank. */
		}

		public void GoBack()
		{
			Frame.GoBack();
		}

		public bool CanGoBack
		{
			get
			{
				return Frame.CanGoBack;
			}
		}

		public void Navigate(Uri source)
		{
			ArgumentValidator.AssertNotNull(source, "source");

			Frame.Navigate(source);
		}

		public void Navigate(string relativeUrl)
		{
			ArgumentValidator.AssertNotNullOrWhiteSpace(relativeUrl, "relativeUrl");

			Uri source = new Uri(relativeUrl, UriKind.Relative);
			Frame.Navigate(source);
		}

		public void Navigate(Type pageType, object parameter = null)
		{
			ArgumentValidator.AssertNotNull(pageType, "pageType");
			if (parameter != null)
			{
				Frame.Navigate(pageType, parameter);
			}
			else
			{
				Frame.Navigate(pageType);
			}
		}

		public Uri Source
		{
			get
			{
				return Frame.CurrentSource;
			}
		}
	}
}
